﻿using CloudStorageLight.Core;
using CloudStorageLight.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;
using Microsoft.AspNet.Identity.Owin;
using System.Threading.Tasks;
using CloudStorageLight.Core.Recommender;
using Microsoft.WindowsAzure;
using CloudStorageLight.Core.Web;

namespace CloudStorageLight.Web.Controllers
{
    [Authorize]
    public class MyPageController : Controller
    {
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }
        private ApplicationUserManager _userManager;

        //protected LogService CreateLogService()
        //{
        //    return new LogService(SystemSettings.Instance.BlobStorage);
        //}

        protected  BlobService CreateBlobService()
        {
            return new BlobService();
        }

        protected  AppBlobUser GetCurrentUser()
        {
            var user = new AppBlobUser(System.Web.HttpContext.Current.User);
            return user;
        }

        // GET: MyPage
        public async Task<ActionResult> Index()
        {
            var user = await UserManager.FindByEmailAsync(System.Web.HttpContext.Current.User.Identity.Name);
            var role = await UserManager.GetRolesAsync(user.Id);
            var domain = BlobService.GetCurrentDomain();

            var vm = new MyPageVM();
            vm.User = new AppBlobUser(user, role.ToArray());

            using (var svc = CreateBlobService())
            {
            var list = await svc.ListRecentAsync(vm.User, 5);
            vm.Recent = list.Select(x => new MyPageVMItem { Path = ConvertLogPathToLink(domain, x.Path), LatestAccess = (DateTime)x.Tag }).ToList();

            var info = svc.GetUserInfo(GetCurrentUser());
            if (info.RecommendedList != null)
            {
                vm.Recommended = ((IRecommendedUser)info).RecommendedList
                    .Select(x => new MyPageVMItem { Path = ConvertLogPathToLink(domain, x) }).ToList();
            }
            return View(vm);

            }
        }

        private string ConvertLogPathToLink(string domain, string logPath)
        {
            if (string.IsNullOrEmpty(domain))
            {
                return Url.RouteUrl("files", new { id = logPath });
            }
            else 
            {
                return Url.RouteUrl("files", new { domain = domain, id = logPath });
            }
        }

        private string ConvertLogPathToLinkName(string domain,  string logPath)
        {
            return BlobUtil.UnescapeDataString(ConvertLogPathToLink(domain, logPath));
        }

    }
}